<template>
  <div class="addRoles">
    <el-form ref="refAgentRoles" size="small" :model="modelAgentRoles" :rules="rulesAgentRoles" :label-position="labelPositionAgentRoles" label-width="100px">
      <el-form-item label="角色名称：" prop="name">
        <el-input v-model="modelAgentRoles.name" placeholder="请输入代理角色名称" clearable />
      </el-form-item>
      <el-form-item label="级别：" prop="agentRoleLevels">
        <el-select v-model="modelAgentRoles.agentRoleLevels" placeholder="请选择代理商角色" style="width: 100%;">
          <el-option v-for="item in agentRoleLevelsOption" :key="item.level" :label="item.name" :value="item.level" />
        </el-select>
      </el-form-item>
      <el-form-item label="创建时间：" v-if="isCreateRolesDialog && modelAgentRoles.createDateUTC">
        <el-input v-model="modelAgentRoles.createDateUTC" placeholder="请输入代理角色名称" readonly disabled clearable />
      </el-form-item>
      <div class="dialog-footer">
        <div class="subBtnWid">
          <el-button type="primary" size="small" @click="onAgentRolesSubmit('refAgentRoles')">确定</el-button>
        </div>
        <div class="subBtnWid">
          <el-button size="small" @click="close">取消</el-button>
        </div>
      </div>
    </el-form>
  </div>
</template>

<script>
import { getAgentRoleLevelsApi, POSTAddAgentRolesApi } from '@/api/agent'
export default {
  name: 'AddRoles',
  props: {
    editAgentRolesInfo: {
      type: Object,
      default() {
        return {}
      }
    },
    // 0 => 创建，1 => 编辑
    isCreateRolesDialog: {
      type: Number,
      default: 0
    }
  },
  data() {
    return {
      modelAgentRoles: {
        name: '',
        agentRoleLevels: '',
        createDateUTC: ''
      },
      rulesAgentRoles: {
        name: [
          { required: true, message: '请输入角色名称', trigger: 'blur' }
        ],
        agentRoleLevels: [
          { required: true, message: '请选择上级代理', trigger: 'change' }
        ]
      },
      labelPositionAgentRoles: 'right',
      rolesLevel: []
    }
  },
  computed: {
    agentRoleLevelsOption() {
      return this.rolesLevel.map((item) => {
        return { level: item.level, name: item.name }
      })
    }
  },
  watch: {
    editAgentRolesInfo: {
      handler(newVal) {
        if (this.isCreateRolesDialog && newVal) {
          this.$set(this, 'modelAgentRoles', {
            name: newVal.name,
            agentRoleLevels: newVal.agentLevelId,
            createDateUTC: this.$moment.utc(newVal.createDateUTC).tz(this.$moment.tz.guess()).format('YYYY-MM-DD HH:mm:ss')
          })
        }
      },
      immediate: true
    }
  },
  created() {
    if (this.rolesLevel && this.rolesLevel.length === 0) {
      getAgentRoleLevelsApi({
        PageNumber: 1,
        PageSize: 30
      }).then(res => {
        this.$set(this, 'rolesLevel', res)
      }).catch(() => {
        this.$message.error('获取上级代理失败！')
      })
    }
  },
  methods: {
    // 提交
    onAgentRolesSubmit(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          if (!(this.isCreateRolesDialog && Object.keys(this.editAgentRolesInfo).length !== 0)) {
            POSTAddAgentRolesApi({
              name: this.modelAgentRoles.name,
              agentLevelId: this.modelAgentRoles.agentRoleLevels
            }).then(() => {
              this.$emit('addAgentRolesModal')
              this.close()
              this.$message.success('代理商角色添加成功！')
            }).catch((err) => {
              this.close()
              this.$message.error(err.response.data || '代理商角色添加失败！')
            })
          }
        } else {
          this.$message.error('验证未通过！')
          return false
        }
      })
    },
    // 取消
    close() {
      this.$emit('close')
    }
  }
}
</script>

<style lang="scss">
  .el-dialog__wrapper {
    .dialogRolesCls {
      max-width: 960px !important;
      width: 100% !important;
    }
  }
</style>

<style lang="scss" scoped>
  .addRoles {
    .dialog-footer {
      text-align: right;
    }
  }
</style>
